<?php
namespace backend\controllers;
/** 
 * @Description:  
 * @Author: Zheng_Great
 * @Date: 2020-05-07 13:20:45
 */
use Yii;
use yii\web\Controller;
use yii\web\HttpException;
use backend\controllers\common\BaseController;

use backend\models\stat\StatDailySite;

class ChartsController extends BaseController
{
	public function queryData(){
		$date_from = $this->get("date_from",date("Y-m-d",strtotime("-30 days") ) );
		$date_to = $this->get("date_to",date("Y-m-d" ) );

		$query = StatDailySite::find();
		$query->where([ '>=','date',$date_from ]);
		$query->andWhere([ '<=','date',$date_to ]);
		// $list = $query->orderBy([ 'id' => SORT_ASC ])->all( );
		$list = $query->orderBy([ 'date' => SORT_ASC ])->all( );
		return $list;
	}	

	// 文章统计
    public function actionIndex(){
		$list=$this->queryData();
		// var_dump($list);
        $data = [
			'categories' => [],
			'series' => [
				[
					'name' => '文章总数',
					'data' => []
				],
				[
					'name' => '当日新增文章数',
					'data' => []
				]
			]
		];

		if( $list ){
			foreach(  $list as $_item ){
				$data['categories'][] = $_item['date'];
				$data['series'][0]['data'][] = floatval( $_item['total_article_count'] );
				$data['series'][1]['data'][] = floatval( $_item['total_new_article_count'] );
			}
		}
      	return $this->renderJSON( $data );
        // return \yii\helpers\Json::encode($data);
	}

	// 博客访问量
	public function actionBlogAccess(){
		$list=$this->queryData();
		$data = [
			'categories' => [],
			'series' => [
				[
					'name' => '博客访问总数',
					'data' => []
				],
				[
					'name' => '当日博客访问量',
					'data' => []
				]
			]
		];
		
		if( $list ){
			foreach(  $list as $_item ){
				$data['categories'][] = $_item['date'];
				$data['series'][0]['data'][] = floatval( $_item['total_blog_count'] );
				$data['series'][1]['data'][] = floatval( $_item['total_new_blog_count'] );
			}
		}
      	return $this->renderJSON( $data );
		// var_dump($this->queryData());
	}

	public function actionTest(){
		$quest = Yii::$app->request;
		$datatime = $quest->get('time');
		// echo $datatime;
		// exit;
		// $datatime ='2020-05-01';
		$date = date('Y-m-d', strtotime($datatime) );
		$date_now = date("Y-m-d H:i:s");
		
		$model_stat_site = new StatDailySite();
		$model_stat_site->date = $date;
		$model_stat_site->created_time = $date_now;
		
		//伪造数据
		$model_stat_site->total_pay_money = mt_rand(500,2010);
		$model_stat_site->total_member_count = mt_rand(500,2050);
		$model_stat_site->total_new_member_count = mt_rand(10,100);
		$model_stat_site->total_article_count = mt_rand(500,3010);
		$model_stat_site->total_new_article_count = mt_rand(20,130);
		$model_stat_site->total_blog_count = mt_rand(500,2310);
		$model_stat_site->total_new_blog_count = mt_rand(50,100);
		
		$model_stat_site->save( 0 );
		return  "it's over ~~" ;

	}	
}